package com.hoshiicloud.user.service.impl.company;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hoshiicloud.common.aop.Logable;
import com.hoshiicloud.common.exception.CommonError;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.user.feign.IdGeneratorService;
import com.hoshiicloud.user.entity.company.ComDepartments;
import com.hoshiicloud.user.mapper.company.IComDepartmentsMapper;
import com.hoshiicloud.user.service.company.IComDepartmentsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * <p>
 * 部门 服务实现类
 * </p>
 *
 * @author WGJ
 * @since 2019-04-22
 */
@Service
public class ComDepartmentsServiceImpl extends ServiceImpl<IComDepartmentsMapper, ComDepartments> implements IComDepartmentsService {
    @Autowired
    private IdGeneratorService idGeneratorService;

    /**
     * @param id
     * @return ComDepartments
     * @description: 根据id查询部门信息
     */
    @Cacheable(cacheNames = "comdepartments", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#id")
    @Override
    public ComDepartments getById(Long id) {
        return baseMapper.selectById(id);
    }


    /**
     * @param
     * @return boolean
     * @description: 新增部门
     */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "comdepartments", allEntries = true)
    @Override
    public long insertDept(ComDepartments entity) {
        String id = idGeneratorService.queryGeneratorId();

        if (id == null) {
            throw new ServiceException(CommonError.error("新增部门失败"));
        }
        entity.setId(Long.parseLong(id));
        entity.setCreatedTime(LocalDateTime.now());
        entity.setModifiedTime(LocalDateTime.now());
        int row = baseMapper.insert(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("新增部门失败"));
        }
        return entity.getId();
    }

    /**
     * @param entity
     * @return boolean
     * @description: 修改部门信息
     */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "comdepartments", allEntries = true)
    @Override
    public boolean updateById(ComDepartments entity) {

        entity.setModifiedTime(LocalDateTime.now());
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("修改部门失败"));
        }
        return true;
    }

    /**
     *@Author:campchen
     *@Description:
     *@Date:下午 03:47 2019/9/11
     *@Param:
     *@return:
     */
    @Override
    @Cacheable(cacheNames = "ComDepartments", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#key")
    @Logable(authName = "后台用户操作-根据公司部门进行分页查询")
    public IPage<ComDepartments> getPage(IPage<ComDepartments> page, QueryWrapper<ComDepartments> queryWrapper, String key) {
        return baseMapper.selectPage(page,queryWrapper);
    }
}
